/*
 * PropertiesGlassPanel.java
 *
 * Created on April 20, 2007, 8:26 PM
 */

package org.voidness.twittava.gui.glass;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.voidness.twittava.config.ConfigurationManager;
import org.voidness.twittava.gui.TwittavaWindow;

/**
 *
 * @author  void
 */
public class PropertiesGlassPanel extends GlassPanel {
    
    /** Creates new form PropertiesGlassPanel */
    public PropertiesGlassPanel(TwittavaWindow parent) {
        initComponents();
        this.parentWindow = parent;
        
        setupData();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        propertiesPanel = new javax.swing.JPanel();
        connectionProperties = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        passwordTextField = new javax.swing.JPasswordField();
        buttonsPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        propertiesPanel.setLayout(new java.awt.BorderLayout());

        propertiesPanel.setOpaque(false);
        connectionProperties.setBorder(javax.swing.BorderFactory.createTitledBorder("Connection"));
        connectionProperties.setOpaque(false);
        jLabel1.setText("Username:");

        jLabel2.setText("Password:");

        passwordTextField.setText("jPasswordField1");

        javax.swing.GroupLayout connectionPropertiesLayout = new javax.swing.GroupLayout(connectionProperties);
        connectionProperties.setLayout(connectionPropertiesLayout);
        connectionPropertiesLayout.setHorizontalGroup(
            connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionPropertiesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(24, 24, 24)
                .addGroup(connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(passwordTextField)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                .addContainerGap(150, Short.MAX_VALUE))
        );
        connectionPropertiesLayout.setVerticalGroup(
            connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionPropertiesLayout.createSequentialGroup()
                .addGroup(connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectionPropertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(197, Short.MAX_VALUE))
        );
        propertiesPanel.add(connectionProperties, java.awt.BorderLayout.CENTER);

        add(propertiesPanel, java.awt.BorderLayout.CENTER);

        buttonsPanel.setOpaque(false);
        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        buttonsPanel.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        buttonsPanel.add(cancelButton);

        add(buttonsPanel, java.awt.BorderLayout.SOUTH);

    }// </editor-fold>//GEN-END:initComponents

    private void hideMe() {
        this.setVisible(false);
        this.parentWindow.setupGlassPane();
        this.parentWindow.setupApplication();
    }
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        hideMe();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        boolean result = true;
        
        result &= checkAndSaveMandatory("username", usernameTextField.getText());
        result &= checkAndSaveMandatory("password", passwordTextField.getText());
        
        if (result) {
            ConfigurationManager.getInstance().save();
            hideMe();
        } else {
            JOptionPane.showMessageDialog(this, "There is a problem with the entered data. Check for invalid fields.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void setupData() {
        usernameTextField.setText(ConfigurationManager.getInstance().getProperty("username"));
        passwordTextField.setText(ConfigurationManager.getInstance().getProperty("password"));
    }

    private boolean checkAndSaveMandatory(String key, String value) {
        if (value != null && !"".equals(value)) {
            ConfigurationManager.getInstance().setProperty(key, value);
            return true;
        }
        return false;
    }    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel connectionProperties;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton okButton;
    private javax.swing.JPasswordField passwordTextField;
    private javax.swing.JPanel propertiesPanel;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables

    private TwittavaWindow parentWindow;
    
}
